package com.hqyj.servletdemo.util;

import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.digest.DigestAlgorithm;
import cn.hutool.crypto.digest.Digester;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MyUtil {


    private static String mySalt="https://www.myproject.net";


    //检查是否是合法的手机号码
    public static boolean isMobileNumber(String mobileStr) {
        Pattern p = Pattern.compile("^((13[0-9])|(15[^4])|(18[0-9])|(17[0-9])|(19[0-9])|(147))\\d{8}$");
        Matcher m = p.matcher(mobileStr);
        return m.matches();
    }

    public static boolean isEmail(String email) {
        // return ReUtil.isMatch("^(\\w+([-.][A-Za-z0-9]+)*){3,18}@\\w+([-.][A-Za-z0-9]+)*\\.\\w+([-.][A-Za-z0-9]+)*$",email);
        //正则表达式匹配任何字符
        String regex = "^[\\w!#$%&'*+/=?`{|}~^-]+(?:\\.[\\w!#$%&'*+/=?`{|}~^-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,6}$";
        //编译正则表达式
        Pattern pattern = Pattern.compile(regex);
        //检索匹配器对象
        Matcher m = pattern.matcher(email);
        return m.matches();
    }

    //随机生成一个用户名
    public static String generateRandomUsername() {
        return IdUtil.simpleUUID();
    }

    //使用MD5+盐的摘要算法对注册用户的密码进行摘要加密

    public static String encodePasswordByMd5Salt(String password) throws Exception{
        Digester md5 = new Digester(DigestAlgorithm.MD5);
        //给MD5添加了盐
        System.out.println("您使用的盐是：" + MyUtil.mySalt);
        md5.setSalt(mySalt.getBytes("utf-8"));
        return md5.digestHex(password);
    }


    public static void main(String[] args) throws Exception {


    }
}


